//
//  GXShopBrowseController.m
//  YIChuSeller
//
//  Created by 田小椿 on 2018/12/2.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//

#import "GXShopBrowseController.h"
#import "GXShopBrowseView.h"
#import "GXShopSortCell.h"
#import "GXAddContentCell.h"
#import "GXAddContentModel.h"
#import "GXShopBrowseDetailController.h"
#import "GXMenuGroupModel.h"
@interface GXShopBrowseController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
@property (nonatomic,strong)UITableView * sortTableView;
@property (nonatomic,strong)NSMutableArray * sortDataArray;
@property (nonatomic,strong)NSMutableArray * contentDataArray;
@property (nonatomic,strong)UITableView * contentTableView;
@property (nonatomic,strong) GXShopBrowseView * headView;
@property (nonatomic, strong) NSIndexPath * currentIndexPath;

@end
static NSString * const shopSortCell=@"GXShopSortCell";
static NSString * const addContentCell=@"GXAddContentCell";
@implementation GXShopBrowseController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestGroupRequsetData];
    [self creatAllView];
    _currentIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
}
- (void)creatAllView
{
    self.view.backgroundColor =kGlobalbgColor;
    [self.view addSubview:self.headView];
    [self.view addSubview:self.sortTableView];
    [self.view addSubview:self.contentTableView];
    NSDictionary *shopDic = [[NSUserDefaults standardUserDefaults]objectForKey:@"ShopDetail"];
    self.headView.contentDic = shopDic;
    
}
#pragma 请求分组管理数据
- (void)requestGroupRequsetData
{
    [MenuContentManager selectMenuGroupWithStoreID:StoreId success:^(id responseObject) {
        if (responseObject) {
            [self handleMessageData:responseObject];
        }
    } fail:^(id responseObject) {
        
    }];
}
#pragma 处理分组数据
- (void)handleMessageData:(id)responseObj
{
    self.sortDataArray= [NSMutableArray arrayWithArray:responseObj];
    [self.sortTableView reloadData];
    GXMenuGroupModel *model = self.sortDataArray.firstObject;
    [self changeContentTableViewDataSourceFromSortTableViewItemId:model.dishesId];
}
- (void)changeContentTableViewDataSourceFromSortTableViewItemId:(NSString *)itemIdStr
{
     self.contentDataArray = [NSMutableArray array];
    for (int i= 0; i<self.rightDataSource.count; i++) {
        NSDictionary *content = self.rightDataSource[i];
        if ([itemIdStr isEqualToString:content[@"dishesId"]]) {
            GXAddContentModel *model = [GXAddContentModel mj_objectWithKeyValues:content];
            [self.contentDataArray addObject:model];
        }
    }
    if (!self.contentDataArray.count) {
        [SVProgressHUD showImage:nil status:@"暂无数据"];
    }
    [self.contentTableView reloadData];
}
#pragma GXShopBrowseView
- (GXShopBrowseView *)headView
{
    if (_headView == nil) {
        _headView  = [GXShopBrowseView shopBrowseView];
         _headView.frame = CGRectMake(0, -Height_StatusBar, kUIScreenWidth, 0.35*kUIScreenHeight);
    }
    return _headView;
}

- (UITableView *)sortTableView
{
    if (_sortTableView == nil) {
        _sortTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0.35*kUIScreenHeight-Height_StatusBar, 60, kUIScreenHeight) style:UITableViewStyleGrouped];
        [self setTableViewProperty:_sortTableView];
        [_sortTableView registerNib:[UINib nibWithNibName:@"GXShopSortCell" bundle:nil] forCellReuseIdentifier:shopSortCell];
    }
    return _sortTableView;
}
- (UITableView *)contentTableView
{
    if (_contentTableView == nil) {
        _contentTableView = [[UITableView alloc]initWithFrame:CGRectMake(61, 0.35*kUIScreenHeight-Height_StatusBar,kUIScreenWidth-61, kUIScreenHeight-0.35*kUIScreenHeight-Height_StatusBar+Height_TabBar) style:UITableViewStyleGrouped];
        [self setTableViewProperty:_contentTableView];
        [_contentTableView  registerNib:[UINib nibWithNibName:@"GXAddContentCell" bundle:nil] forCellReuseIdentifier:addContentCell];
    }
    return _contentTableView;
}
- (void)setTableViewProperty:(UITableView *)tableView
{
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
    tableView.separatorStyle =UITableViewCellSeparatorStyleNone;
    tableView.sectionFooterHeight = 0.01f;
    tableView.sectionHeaderHeight= 0.01f;
    tableView.showsHorizontalScrollIndicator=NO;
    tableView.showsVerticalScrollIndicator=NO;
    tableView.backgroundColor = [UIColor whiteColor];
}


#pragma mark ---UITableViewDataSource UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.sortTableView) {
         return self.sortDataArray.count;
    }else{
        return self.contentDataArray.count;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.contentTableView) {
        return 75;
    }
    return 40;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.sortTableView) {
        GXShopSortCell * cell = [tableView dequeueReusableCellWithIdentifier:shopSortCell];
        GXMenuGroupModel *model = self.sortDataArray[indexPath.row];
            cell.title = [NSString stringWithFormat:@"%@",model.name];
        if (indexPath == _currentIndexPath) {
            cell.nameLabel.textColor = [UIColor blackColor];
        }else{
            cell.nameLabel.textColor = [UIColor lightGrayColor];
        }
        return cell;
    }
    GXAddContentCell * cell = [tableView dequeueReusableCellWithIdentifier:addContentCell];
    GXAddContentModel *model = self.contentDataArray[indexPath.row];
    if (model) {
        cell.model = model;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.sortTableView){
        _currentIndexPath = indexPath;
        [tableView reloadData];
        GXMenuGroupModel *model = self.sortDataArray[indexPath.row];
        if (![NSString isBlankString:model.dishesId]) {
            [self changeContentTableViewDataSourceFromSortTableViewItemId:model.dishesId];
        }
    }
    if (tableView == self.contentTableView) {
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
        GXAddContentModel *model = self.contentDataArray[indexPath.row];
        GXShopBrowseDetailController * detailVC = [[GXShopBrowseDetailController alloc]init];
        if (![NSString isBlankString:model.serviceId]){
            detailVC.serviceIdString = model.serviceId;
        }
        [self.navigationController pushViewController:detailVC animated:YES];
    }
}

#pragma mark -- UIScrollViewDelegate
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

@end
